﻿
using ProductMonitor.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor.UserControls
{
    /// <summary>
    /// MonitorUC.xaml 的交互逻辑
    /// </summary>
    public partial class MonitorUC : UserControl
    {
        public MonitorUC()
        {
            InitializeComponent();

            // 订阅 ViewModel 的事件
            if (this.DataContext is ProductMonitor.ViewModels.MonitorUCVM vm)// 检查 DataContext 是否为 MonitorUCVM 类型
            {
                vm.ShowDetailRequested += Vm_ShowDetailRequested;// 直接订阅事件
            }
            else
            {
                this.DataContextChanged += (s, e) =>// 当 DataContext 改变时订阅事件
                {
                    if (e.NewValue is ProductMonitor.ViewModels.MonitorUCVM newVm)// 检查新的 DataContext 是否为 MonitorUCVM 类型
                    {
                        newVm.ShowDetailRequested += Vm_ShowDetailRequested;// 订阅事件
                    }
                };

            }
        }

        private void Vm_ShowDetailRequested()
        {
            MainWindow mainWindow = Window.GetWindow(this) as MainWindow;// 获取当前窗口的 MainWindow 实例
            mainWindow?.ShowDetailPage();// 调用 MainWindow 的 ShowDetailPage 方法来显示详细信息页面

        }

        private void SeSetting_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindow settingsWindow = new SettingsWindow();// 创建设置窗口实例
            settingsWindow.ShowDialog();// 显示设置窗口

        }
    }
}
